/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.cashmate.struts;

import com.cashmate.hibernate.dao.EmployeeDAO;
import com.cashmate.hibernate.pojo.Employees;
import com.opensymphony.xwork2.ActionSupport;
import java.io.InputStream;
import java.io.StringBufferInputStream;
import org.apache.log4j.Logger;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;

/**

 * @author Dean
 */
@Result(name = "error", type = "redirect", location = "../../messages.jsp")
public class ChangePassword extends ActionSupport {

    private Logger logger = Logger.getLogger(ChangePassword.class);
    private String newpassword;
    private String securityquestion;
    private String oldpassword;
    private InputStream inputStream;
    private String securityanswer;
    private EmployeeDAO emp = (EmployeeDAO) ServletActionContext.getServletContext().getAttribute("employeeDAO");
    private Employees emps = (Employees) ServletActionContext.getRequest().getSession().getAttribute("employee");

    @Action(value = "/change-password-new", results = {
        @Result(name = "success", type = "stream", params = {"inputName", "inputStream", "contentType", "text/html"})
    })
    public String newChangePassword() {
        try {
            logger.info(newpassword + "," + securityquestion + "," + securityanswer + "," + oldpassword);
            Employees e = emp.get(emps.getEmployeeid());
            if (securityquestion != null && oldpassword == null) {
                e.setSecurityanswer(securityanswer);
                e.setSecurityquestion(securityquestion);
                e.setPassword(newpassword);
                emp.update(e);
                inputStream = new StringBufferInputStream("<script type=\"text/javascript\">$('div#changeformResult').html(info('Password Changed...'));$('#dialogClose').show();</script>");
                logger.info(emps.getName() + " updated his/her password.");
                ServletActionContext.getRequest().getSession().removeAttribute("employee");
                ServletActionContext.getRequest().getSession().setAttribute("employee", e);
            }
            if (oldpassword != null && securityquestion == null) {
                if (newpassword.equals(e.getPassword())) {
                    inputStream = new StringBufferInputStream("<script type=\"text/javascript\">$('#changePasswordForm').hide();$('div#changeformResult').addClass('ui-state-highlight').removeClass('ui-state-error');$('div#changeformResult').html(info('Password Changed...'));$('#dialogClose').show();</script>");
                }
                if (!oldpassword.equals(e.getPassword())) {
                    inputStream = new StringBufferInputStream("<p style=\"text-align:center;\"><span class=\"ui-icon ui-icon-alert\" style=\"text-align:center;float: left; margin-right: .3em;\"></span><strong>Invalid Old Password.</strong></p>"
                            + "<script type=\"text/javascript\">$('#changeformResult').addClass('ui-state-error');$('#changePasswordForm').show();$('#dialogClose').hide();setError('oldpassword','changePasswordForm');</script>");
                } else {
                    e.setPassword(newpassword);
                    emp.update(e);
                    inputStream = new StringBufferInputStream("<script type=\"text/javascript\">$('#changePasswordForm').hide();$('div#changeformResult').addClass('ui-state-highlight').removeClass('ui-state-error');$('div#changeformResult').html(info('Password Changed...'));$('#dialogClose').show();</script>");
                    logger.info(emps.getName() + " updated his/her password.");
                    ServletActionContext.getRequest().getSession().removeAttribute("employee");
                    ServletActionContext.getRequest().getSession().setAttribute("employee", e);
                }
            }
        } catch (Exception e) {
            if (emps != null) {
                logger.fatal("(" + emps.getIdnumber() + "):" + e.getLocalizedMessage(), e);
            } else {
                logger.fatal(e.getLocalizedMessage(), e);
            }
            e.printStackTrace();
            addActionError(e.getLocalizedMessage());
            return ERROR;
        }
        return SUCCESS;
    }

    public void setNewpassword(String newpassword) {
        this.newpassword = newpassword;
    }

    public void setSecurityanswer(String securityanswer) {
        this.securityanswer = securityanswer;
    }

    public void setSecurityquestion(String securityquestion) {
        this.securityquestion = securityquestion;
    }

    public void setOldpassword(String oldpassword) {
        this.oldpassword = oldpassword;
    }

    public InputStream getInputStream() {
        return inputStream;
    }
}
